﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NXTMessaging;

namespace WindowsGame1.gamePlugins.nxt
{
    class Receiving
    {
        private Thread thread;
        private Thread thread2;
        private int delay = 20;
        private ControllerBridgeNXT root;
        private static float viewPortY;


        public Receiving(ControllerBridgeNXT root)
        {
            this.root = root;
            viewPortY = root.game.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
            this.thread = new Thread(new ThreadStart(run));
            this.thread2 = new Thread(new ThreadStart(run2));
            this.thread.Priority = ThreadPriority.AboveNormal;
            this.thread2.Priority = ThreadPriority.AboveNormal;
            this.thread.Start();
            this.thread2.Start();
        }

        protected void run2()
        {
            while (root.successfullConnectAndAlive)
            {
                if (root.successfullConnectAndAlive)
                {
                    float message2 = 0;
                    NXTCommandResult resultTwo = root.messenger.ReceiveMessage(root.player2MailBox, out message2);
                    if (resultTwo == NXTCommandResult.Success)
                    {
                        /*float degrees = message;
                        double ratio = (double)degrees / (double)viewPortY;
                        root.betje2 = (int)(viewPortY * ratio);*/
                        float degrees = message2;
                        root.currentDegrees2 = (int)message2;
                        //Console.WriteLine(message2);
                        root.maxdegreescount2 = root.calibrateP2Right - root.calibrateP2Left;



                        //double ratio = (double)(degrees) / (double)viewPortY;
                        if (root.maxdegreescount2 != 0)
                            root.betje2 = (int)(0.5 * (degrees - root.calibrateP2Left)) * (root.maxHeight / root.maxdegreescount2);
                    }
                }
            }
        }

       protected void run() {
           while (root.successfullConnectAndAlive)
           {
               if (root.successfullConnectAndAlive)
               {
                    float message1 = 0;
                    //float message2 = 0;
                    //Player 1
                    NXTCommandResult resultOne = root.messenger.ReceiveMessage(root.player1MailBox, out message1);
                    if (resultOne == NXTCommandResult.Success)
                    {
                        float degrees = message1;
                        root.currentDegrees1 = (int)message1;
                        root.maxdegreescount1 = root.calibrateP1Right - root.calibrateP1Left;

                        //double ratio = (double)(degrees) / (double) viewPortY;
                        if (root.maxdegreescount1 != 0)
                            root.betje1 = (int)(0.5 * (degrees - root.calibrateP1Left)) * (root.maxHeight / root.maxdegreescount1);
                    }
                    //if (root.game.gameState == Game1.GameState.InMPGame) //add calibrate
                    //{
                        //Player 2

                    //}
                }
                Thread.Sleep(delay);
            }
        }

        public Thread getThread()
        {
            return thread;
        }
        public Thread getThread2()
        {
            return thread2;
        }
    }
}
